import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:dk_flutter_base/src/core/util/check_util.dart';

///base64解码
String dkDecodeBase64(String base64) {
  if (stringEmpty(base64)) {
    return "";
  }

  /// 后台+/ 替换成 -_  移动端 -_ 替换成 +/
  base64.replaceAll('-', '+');
  base64.replaceAll('_', '/');

  int remain = base64.length % 4;
  if (remain > 0) {
    base64 += "====".substring(remain);
  }
  return String.fromCharCodes(base64Decode(base64));
}

///base64编码
String dkEncodeBase64(String toBase64) =>
    stringEmpty(toBase64) ? "" : base64Encode(utf8.encode(toBase64))
      ..replaceAll('+', '-')
      ..replaceAll('/', '_')
      ..replaceAll("=", "");

/// md5 加密
String generateMd5(String content) {
  return md5.convert(utf8.encode(content)).toString();
}
